package project.cscall.android;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CS_Call extends FragmentActivity implements ActionBar.TabListener {

	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
	private Button btnStartGoi;
	private EditText edtcallnumber;
	private EditText edtnumberofcall;
	private EditText edtduration;
	private EditText edtpause;
	//su kien click nut start trong tab goi
	public void OnButtonClick(View v)
    {
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri
				.parse(edtcallnumber.getText().toString()));
		startActivity(callIntent);
		Toast toast = Toast.makeText(getApplicationContext(),
				"Dialing...", Toast.LENGTH_LONG);
		toast.show();
    } 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout__cs__call);
		View inflatedView = getLayoutInflater().inflate(R.layout.goi, null);
		btnStartGoi = (Button) inflatedView.findViewById(R.id.btnstartgoi);
		edtcallnumber = (EditText) inflatedView
				.findViewById(R.id.edtcallnumber);
		edtnumberofcall = (EditText) inflatedView
				.findViewById(R.id.edtnumberofcall);
		edtduration = (EditText) inflatedView.findViewById(R.id.edtduration);
		edtpause = (EditText) inflatedView.findViewById(R.id.edtpause);
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// View ViewGoi = getLayoutInflater().inflate(R.layout.goi, null);
		// View Viewnghe = getLayoutInflater().inflate(R.layout.nghe, null);
		// For each of the sections in the app, add a tab to the action bar.
		actionBar.addTab(actionBar.newTab().setText(R.string.tab_goi)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.tab_Nghe)
				.setTabListener(this));		
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(
					savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
				.getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.layout__cs__call, menu);
		return true;
	}

	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		// When the given tab is selected, show the tab contents in the
		// container
		Fragment fragment = new DummySectionFragment();
		Bundle args = new Bundle();
		args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
				tab.getPosition() + 1);
		fragment.setArguments(args);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, fragment).commit();
		// Sự kiện click button start
		btnStartGoi.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {

				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri
						.parse(edtcallnumber.getText().toString()));
				startActivity(callIntent);
				Toast toast = Toast.makeText(getApplicationContext(),
						"Dialing...", Toast.LENGTH_LONG);
				toast.show();
			}
		});

	}

	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public static class DummySectionFragment extends Fragment {
		public DummySectionFragment() {
		}

		public static final String ARG_SECTION_NUMBER = "section_number";

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			Bundle args = getArguments();
			// Integer.parseInt(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
			if (Integer.parseInt(Integer.toString(args
					.getInt(ARG_SECTION_NUMBER))) == 1) {
				View mainView = inflater
						.inflate(R.layout.goi, container, false);
				return mainView;
			} else {
				View mainView = inflater.inflate(R.layout.nghe, container,
						false);
				return mainView;
			}

		}
	}

	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}
}
